# coding=utf-8
import logging
from itpay.core.agency import agency_service
from itpay.core.mch import service as mch_service

_MAESTRO = logging.getLogger('maestro')


def track_maestro_agency_recharge_success(apply_id, event_time, agency_id, amount):
    """
    子代申请信用额度成功事件
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)
    _MAESTRO.info({
        '_id': str(apply_id),
        '_event_id': 'agency_recharge_success',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        'amount': amount / 100
    })


def track_maestro_user_recharge_create(recharge_id, event_time, agency_id, amount, mch_id, user_id):
    """
    用户充值申请事件
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)

    _MAESTRO.info({
        '_id': str(recharge_id),
        '_event_id': 'user_recharge_create',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        '_mch_id': mch_id,
        '_user_id': user_id,
        'amount': amount / 100
    })


def track_maestro_user_recharge_success(recharge_id, event_time, agency_id, amount, mch_id, user_id):
    """
    用户充值成功事件
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)

    _MAESTRO.info({
        '_id': str(recharge_id),
        '_event_id': 'user_recharge_success',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        '_mch_id': mch_id,
        '_user_id': user_id,
        'amount': amount / 100
    })


def track_maestro_user_recharge_chase(recharge_id, event_time, agency_id, amount, mch_id, user_id):
    """
    用户充值追分事件
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)

    _MAESTRO.info({
        '_id': str(recharge_id),
        '_event_id': 'user_recharge_chase',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        '_mch_id': mch_id,
        '_user_id': user_id,
        'amount': amount / 100
    })


def track_maestro_user_recharge_comment(comment_id, event_time, agency_id, mch_id, user_id, comment_star,
                                        comment_rank, auto_comment
                                        ):
    """
    用户充值评论
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)

    _MAESTRO.info({
        '_id': str(comment_id),
        '_event_id': 'user_recharge_comment',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        '_mch_id': mch_id,
        '_user_id': user_id,
        'auto_comment': 1 if auto_comment else 0,
        'comment_star': comment_star,
        'comment_rank': comment_rank,
    })


def track_maestro_agency_complain(complain_id, event_time, agency_id, mch_id, user_id, complain_types):
    """
    用户投诉
    :return:
    """
    agency = agency_service.get_agency_by_id(agency_id)

    _MAESTRO.info({
        '_id': str(complain_id),
        '_event_id': 'user_recharge_complain',
        '_event_time': event_time,
        '_agency_id': agency_id,
        '_parent_id': agency['parent_id'],
        '_mch_id': mch_id,
        '_user_id': user_id,
        'complain_type': complain_types
    })


def track_maestro_mch_create(mch_id, event_time):
    """
    新增商户
    :return:
    """
    mch = mch_service.get_mch(mch_id)

    _MAESTRO.info({
        '_id': str(mch_id),
        '_event_id': 'mch_create',
        '_event_time': event_time,
        'mch_name': mch['name'],
        'mch_type': mch['mch_type'],
        'mch_id': mch_id
    })
